package com.controller.admin;

import com.domain.Blogger;
import com.service.BloggerService;
import com.util.Const;
import com.util.DateUtil;
import com.util.ResponseUtil;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
@Log4j2
@Controller
@RequestMapping("/admin/blogger")
public class BloggerAdminController {
    @Resource
    private BloggerService bloggerService;
    @RequestMapping("/save.do")
    public String save(@RequestParam(value = "imageFile",required = false) MultipartFile imageFile, Blogger blogger, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (!imageFile.isEmpty()) {
            //String filePath = request.getServletContext().getRealPath("/static/userImages");
            String filePath="E:\\Java\\ssmblog\\userImages";
            String imageName = DateUtil.getCurrentDateStr() + "." + imageFile.getOriginalFilename().split("\\.")[1];
            File targetFile = new File(filePath,imageName);
            imageFile.transferTo(targetFile);
            blogger.setImageName(imageName);
        }
        int resultTotal = bloggerService.update(blogger);
        StringBuffer result = new StringBuffer();
        if (resultTotal > 0) {
            result.append("<script language='javascript'>alert('修改成功');</script>");
        } else {
            result.append("<script language='javascript'>alert('修改失败');</script>");
        }
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/find")
    public String find(HttpServletResponse response) throws IOException {
        Blogger blogger = (Blogger) SecurityUtils.getSubject().getSession().getAttribute(Const.CURRENT_USER);
        JSONObject jsonObject = JSONObject.fromObject(blogger);
        log.info(blogger);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("/modifyPassword.do")
    public String modifyPassword(@RequestParam("id") String id, @RequestParam("newPassword") String newPassword, HttpServletResponse response) throws IOException {
        Blogger blogger = new Blogger();
        blogger.setId(Integer.valueOf(id));
        blogger.setPassword(newPassword);
        Integer total = bloggerService.update(blogger);
        log.info(blogger);
        JSONObject result = new JSONObject();
        if (total > 0) {
            result.put("success", Boolean.TRUE);
        } else {
            result.put("success", Boolean.FALSE);
        }
        ResponseUtil.write(response, result);
        return null;
    }
@RequestMapping("/logout.do")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/login.jsp";
    }

}
